package baidu.reader.common.ui
{
    import baidu.reader.common.*;
    import baidu.reader.common.effects.*;
    import baidu.reader.common.model.*;
    import baidu.reader.common.model.VO.*;
    import baidu.reader.common.ui.components.*;
    import baidu.reader.common.ui.toc.*;
    import baidu.ui.controls.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;

    import com.greensock.*;

    import flash.display.*;
    import flash.events.*;

    public class UIMain extends AutoLayoutContainer
    {
        [Embed(source="assets/sprite20.swf")]
        protected var ReaderBG:Class;
        protected var background:Sprite;
        protected var pageNumberDisplay:PageNumber;
        protected var pageNumberInput:PageNumberInputPanel;
        protected var screenModel:ScreenModel;
        protected var _tocContainer:ToCContainer;
        protected var _toolbar:TopToolBar;
        protected var enter_button:Button;

        public function UIMain()
        {
            this.initScreen();
            this.initModel();
            this.init();
        }

        protected function initScreen() : void
        {
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(FullScreenEvent.FULL_SCREEN, this.fullScreenHandler);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResizeHandler);
            this.addEventListener(Event.ADDED_TO_STAGE, this.screenResizeHandler);
            return;
        }

        protected function initModel() : void
        {
            return;
        }

        protected function init() : void
        {
            this.initBG();
            this.initToolBar();
            this.initPageNumber();
            return;
        }

        private function initPageNumber() : void
        {
            this.pageNumberDisplay = new PageNumber();
            addChild(this.pageNumberDisplay);
            var _loc_1:* = this._childsLayoutInfo[this.pageNumberDisplay] as LayoutInfo;
            _loc_1.right = 32;
            _loc_1.bottom = 10;
            this.pageNumberDisplay.addEventListener(ReaderEvent.SHOW_PAGE_NUMBER_INPUT, this.pageNumClickHandler);
            this.pageNumberInput = new PageNumberInputPanel();
            addChild(this.pageNumberInput);
            this.pageNumberInput.visible = false;
            var _loc_2:* = this._childsLayoutInfo[this.pageNumberInput] as LayoutInfo;
            _loc_2.right = 30;
            _loc_2.bottom = 5;
            this.pageNumberInput.addEventListener(FocusEvent.FOCUS_OUT, this.inputFocusOutHandler);
            return;
        }

        protected function initToolBar() : void
        {
            this._toolbar = new TopToolBar();
            addChild(this._toolbar);
            var _loc_1:* = this._childsLayoutInfo[this._toolbar] as LayoutInfo;
            _loc_1.top = 8;
            _loc_1.left = 0;
            _loc_1.widthPercent = 100;
            return;
        }

        protected function initBG() : void
        {
            this.background = new ReaderBG();
            addChild(this.background);
            var _loc_1:* = this._childsLayoutInfo[this.background] as LayoutInfo;
            _loc_1.widthPercent = 100;
            _loc_1.heightPercent = 100;
            return;
        }

        private function screenResizeHandler(event:Event) : void
        {
            var _loc_2:* = 800 / 650;
            if (this.screenModel.stage.stageWidth == 0 || this.screenModel.stage.stageHeight == 0)
            {
                return;
            }
            var _loc_3:* = this.screenModel.stage.stageWidth / this.screenModel.stage.stageHeight;
            if (_loc_3 < _loc_2)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_SINGLE_PAGE));
                (InstanceManager.createSingletonInstance(AppPreferenceModel) as AppPreferenceModel).displayType = AppPreferenceVO.SINGLE_PAGE;
                this.toolbar.displayTypeDisabled = true;
            }
            else
            {
                this.toolbar.displayTypeDisabled = false;
            }
            return;
        }

        protected function fullScreenHandler(event:FullScreenEvent) : void
        {
            this.inputFocusOutHandler(null);
            this.pageNumberDisplay.enabled = !event.fullScreen;
            return;
        }

        protected function inputFocusOutHandler(event:FocusEvent) : void
        {
            TweenLite.to(this.pageNumberDisplay, 0.2, {autoAlpha:1, delay:0.1});
            TweenLite.to(this.pageNumberInput, 0.2, {autoAlpha:0});
            return;
        }

        protected function pageNumClickHandler(event:ReaderEvent) : void
        {
            TweenLite.to(this.pageNumberDisplay, 0.2, {autoAlpha:0});
            TweenLite.to(this.pageNumberInput, 0.2, {autoAlpha:1, delay:0.1});
            this.pageNumberInput.focus();
            return;
        }

        protected function doDisplayPrevPage(param1:DisplayObject) : void
        {
            PageTurnManager.getInstance().startSingleTurn(param1, false);
            PageTurnManager.getInstance().addEventListener(PageEffectEvent.EFFECT_END, this.onPrevEffectEnd);
            return;
        }

        protected function onPrevEffectEnd(event:PageEffectEvent) : void
        {
            PageTurnManager.getInstance().removeEventListener(PageEffectEvent.EFFECT_END, this.onPrevEffectEnd);
            this.hidePageContent();
            addChild(this._tocContainer);
            var _loc_2:* = this._childsLayoutInfo[this._tocContainer] as LayoutInfo;
            _loc_2.left = 20;
            _loc_2.right = 20;
            _loc_2.top = 40;
            _loc_2.bottom = 30;
            return;
        }

        public function hidePageContent() : void
        {
            return;
        }

        protected function doDisplayNextPage(param1:DisplayObject) : void
        {
            PageTurnManager.getInstance().startSingleTurn(param1);
            return;
        }

        public function showToC(param1:Boolean = false) : void
        {
            if (!this._tocContainer)
            {
                this._tocContainer = new ToCContainer();
            }
            this._tocContainer.tocPage.backButton.visible = !param1;
            addChild(this._tocContainer);
            var _loc_2:* = this._childsLayoutInfo[this._tocContainer] as LayoutInfo;
            _loc_2.left = 20;
            _loc_2.right = 20;
            _loc_2.top = 40;
            _loc_2.bottom = 30;
            if (!param1)
            {
                this.drawNow();
                this.doDisplayPrevPage(this._tocContainer);
                removeChild(this._tocContainer);
            }
            TweenLite.to(this.pageNumberDisplay, 0.2, {autoAlpha:0});
            TweenLite.to(this.pageNumberInput, 0.2, {autoAlpha:0});
            this._toolbar.disableAll();
            return;
        }

        public function hideToC() : void
        {
            if (this._tocContainer && this._tocContainer.stage)
            {
                this.doDisplayNextPage(this._tocContainer);
                removeChild(this._tocContainer);
                this._toolbar.restoreAll();
                TweenLite.to(this.pageNumberDisplay, 0.2, {autoAlpha:1});
                TweenLite.to(this.pageNumberInput, 0.2, {autoAlpha:0});
                if (this.enter_button && this.enter_button.parent)
                {
                    this.enter_button.parent.removeChild(this.enter_button);
                }
            }
            return;
        }

        public function setPageNumber(param1:int = 0, param2:int = 0) : void
        {
            if (!isNaN(param1))
            {
                this.pageNumberDisplay.currentPage = param1;
                this.pageNumberInput.pageNumber = param1;
            }
            if (!isNaN(param2))
            {
                this.pageNumberDisplay.totalPage = param2;
            }
            return;
        }

        public function showEnterButton() : void
        {
            this.enter_button = new Button();
            this.enter_button.setStyle("skin",ResourceFactory.EnterButtonSkin);
            this.enter_button.useHandCursor = true;
            this.enter_button.addEventListener(MouseEvent.CLICK, function (event:Event) : void
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_FIRST_PAGE));
                return;
            }
            );
            addChild(this.enter_button);
            var enter_info:* = _childsLayoutInfo[this.enter_button];
            enter_info.offsetCenterH = -50;
            enter_info.bottom = 5;
            return;
        }

        public function get toolbar() : TopToolBar
        {
            return this._toolbar;
        }

    }
}
